package com.victualler.core.common.utils;

import com.victualler.core.base.dao.BaseDao;
import com.victualler.core.common.annotation.InitEditColumn;
import com.victualler.core.common.annotation.ListPageQuery;
import com.victualler.core.common.annotation.ListShowColumn;
import com.victualler.core.common.dto.list.ColumnItemDto;
import com.victualler.core.common.dto.list.FormItemDto;
import com.victualler.core.common.dto.list.InitListPageDto;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.ToIntFunction;

/**
 * @Author: Guoshichang
 * @Date: Create in 19:48 2018-07-08
 * @Discription: 用于页面初始化
 */
public class PageInitUtil {
    /**
     * 根据实体类获取page list页面对应的初始化内容
     * @param queryDtoClass
     * @param columnDtoClass
     * @return
     */
    public static InitListPageDto getInitListPageDto(Class<?> queryDtoClass, Class<?> columnDtoClass){
        List<FormItemDto> queryItemDtoList  = getQueryItemDtoList(queryDtoClass);
        List<ColumnItemDto> columnItemDtoList = getColumnItemDtoList(columnDtoClass);
        return new InitListPageDto(queryItemDtoList,columnItemDtoList);
    }


    /**
     * @author Guoshichang
     * @since 21:08 on 2018/8/8.
     * @param entityClass 实体类
     * @return List 初始化对象
     * @Function 根据实体类注解返回初始化对象
     */
    public static List<FormItemDto> getFormItemDto(Class<? extends BaseDao> entityClass){
        List<FormItemDto> queryItemDtoList = new ArrayList<>();
        //扫字段
        for (Field field : getFields(entityClass)) {
            InitEditColumn initEditColumn= field.getDeclaredAnnotation(InitEditColumn.class);
            if(initEditColumn!=null){
                FormItemDto queryItemDto= new FormItemDto();
                queryItemDto.setTitle(initEditColumn.title());
                queryItemDto.setFieldName(field.getName());
                queryItemDto.setType(initEditColumn.type().getKey());
                queryItemDto.setDictCodeType(initEditColumn.dictCodeType());
                queryItemDto.setCanEdit(initEditColumn.canEdit());
                queryItemDto.setRequired(initEditColumn.required());
                queryItemDto.setSort(initEditColumn.sort());
                queryItemDtoList.add(queryItemDto);
            }
        }
        //排序
        queryItemDtoList.sort(Comparator.comparingInt(new ToIntFunction<FormItemDto>() {
            @Override
            public int applyAsInt(FormItemDto value) {
                return value.getSort();
            }
        }));

        return  queryItemDtoList;
    }


    /**
     * 扫描类中对应对象
     * @param queryDtoClass
     * @return
     */
    private static List<FormItemDto> getQueryItemDtoList(Class<?> queryDtoClass){
        List<FormItemDto> queryItemDtoList = new ArrayList<>();
        //扫字段
        for (Field field : getFields(queryDtoClass)) {
            ListPageQuery  listPageQuery= field.getDeclaredAnnotation(ListPageQuery.class);
            if(listPageQuery!=null){
                FormItemDto queryItemDto= new FormItemDto();
                queryItemDto.setTitle(listPageQuery.title());
                queryItemDto.setFieldName(field.getName());
                queryItemDto.setType(listPageQuery.type().getKey());
                queryItemDto.setDictCodeType(listPageQuery.dictCodeType());
                queryItemDtoList.add(queryItemDto);
            }
        }
        return  queryItemDtoList;
    }

    /**
     * 扫描类中对应对象
     * @param columnDtoClass
     * @return
     */
    private static List<ColumnItemDto> getColumnItemDtoList(Class<?> columnDtoClass){
        List<ColumnItemDto> columnItemDtoList = new ArrayList<>();
        //扫字段
        for (Field field : getFields(columnDtoClass)) {
            ListShowColumn listShowColumn= field.getDeclaredAnnotation(ListShowColumn.class);
            if(listShowColumn!=null){
                //注解转换为对象
                ColumnItemDto columnItemDto = new ColumnItemDto();
                columnItemDto.setTitle(listShowColumn.title());
                columnItemDto.setType(listShowColumn.type().getKey());
                columnItemDto.setWidth(listShowColumn.width());
                columnItemDto.setFieldName(field.getName());
                columnItemDto.setSort(listShowColumn.sort());
                columnItemDto.setDictCodeType(listShowColumn.dictCodeType());
                columnItemDtoList.add(columnItemDto);
            }
        }
        //排序
        columnItemDtoList.sort(Comparator.comparingInt(new ToIntFunction<ColumnItemDto>() {
            @Override
            public int applyAsInt(ColumnItemDto value) {
                return value.getSort();
            }
        }));
        return columnItemDtoList;
    }


    private static List<Field> getFields(Class<?> queryDtoClass){
        List<Field> fields = new ArrayList<>();
        getFields(fields,queryDtoClass);
        return fields;
    }

    private static List<Field> getFields(List<Field> fields,Class<?> queryDtoClass){
        for (Field field : queryDtoClass.getDeclaredFields()) {
            fields.add(field);
        }
        //递归调用
        if(queryDtoClass.getSuperclass() != Object.class){
            getFields(fields,queryDtoClass.getSuperclass());
        }
        return fields;
    }
}
